package com.giscloud.commons.utils;

import java.util.regex.Pattern;

/**
 * @Author:giscloud
 * @Description:
 * @Date: 10:55 2019/10/10
 * @Modified By:
 */
public class NumberUtil {
    /**
     * 验证数字，包括整数、小数、负数等
     * @param input
     * @return
     */
    public static boolean isNumber(Object input) {
        if(input == null) {
            return true;
        }
        if("".equals(input)){
            return false;
        }
        return Pattern.matches("[0-9]*(\\.?)[0-9]*",input.toString());
    }

    /**
     * 四舍五入，保留n位小数
     * @param value
     * @param scale
     * @return
     */
    public static String roundForNumber(String value,int scale){
        if(StringUtils.isBlank(value)){
            return "";
        }
        if(scale < 0){
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        return String.format("%."+scale+"f", Double.valueOf(value));
    }
}
